
local PlayerHeadFrame = require 'View/CommonUI/PlayerHeadFrame'
require 'View/UIBase/UIBase'
require 'Net/FriendBattleHandler'

UIFriendWar  = UIBase.New(PanelResNames.UIFriendWar ,UIBase)
local _M = UIFriendWar
local buyTimes
local battleTimes
local isFree = nil
local costNum = 0
local rankIndex = 0
local myScore = 0
local groupId = 0
local timerId
local seasonTime
local guessTime
local teamTime
local roundTime
local recordInfo
local guessplayer
local nextTime
local guessTimes
local battleTime
local roundStr
local curRound = 0
local fightState
local ceDay
local ceTime
local groupNum
local guessplayer_1
local guessplayer_2
local buyPlayerId = nil
local seasonIndex = 1
local maxindex = nil
local roleId = nil
local roundNum = nil
local outInfo = nil
local godNum
local frameIds = {}
local mobj =  {}
local wraps = {}
local tabId = 0
local curIndex = nil
local inford = nil
local isThrd = nilui
local refreshTimeNum = 0
local timeRefresh = nil
function _M.Init()
    UIBase.Init(_M)
end

function _M:OnCreate()
    self.rankCtrl = self:GetController("rank")
    self.tabCtrl = self:GetController("round")
    self.fightRecord = self:GetController("FightRecord")
    self.mainHead =  self:CreateComponent(PlayerHeadFrame,self:GetChild("playHead"))
    self.heroTxt_L = self:GetChild("title2")
    self.heroTxt_R = self:GetChild("title22")
    self.enemyList = self:GetChild("OpponentList")
    self.challengeNum = self:GetChild("mun2")
    self.roleName = self:GetChild("playname")
    self.rankNum = self:GetChild("textRank")
    self.rankScore = self:GetChild("myscore")
    self.dayTxt =self:GetChild("TimeText3")
    self.place = self:GetChild("place")
    self.placecity = self:GetChild("placecity")
    self.hero_L = self:GetChild("borrow1/head")
    self.hero_L_Ctrl = self:GetController("c1",self:GetChild("borrow1"))
    self.hero_R = self:GetChild("borrow2/head")
    self.hero_R_Ctrl = self:GetController("c1",self:GetChild("borrow2"))
    self.honourRoad = self:GetChild("rankbtn4")
    self.rankList = self:GetChild("ranklist")
    self.reviewList = self:GetChild("listReview")
    self.guessTxt = self:GetChild("title52")
    self.betTip = self:GetChild("betTip")
    self.placeTip = self:GetChild("bt")
    self.strongTxt = self:GetChild("title53")
    self.strongTime = self:GetChild("title54")
    self.teamTime = self:GetChild("time12")
    self.roundTxt = self:GetChild("title55")
    self.groupTxt = self:GetChild("title51")
    self.guessCtrl = self:GetController("fihgting")
    self.seasonPop = self:GetChild("seasonPop")
    self.moneyNum = self:GetChild("txtFightPiont")
    self.myPower = self:GetChild("fightPoint")
    self.changeTitle = self:GetChild("mun")
    self.defendPower = self:GetChild("myDefend")
    self.enemyState = self:GetController("enemyState")
    self.text_money = self:GetChild("n788/title");
    self.roadTxt = self:GetChild("n413")
    self.btn_season = self:GetChild("RankBtnsea1")
    self.guesseffect = self:GetChild("effect_czan")
    self.herotip = self:GetChild("n87")
    self.mainReward = self:GetChild("mainReward")
    self.battleCordCtrl = self:GetController("nothing")
    for i=1,9 do
        if not self.replay then
            self.replay = {}
        end
        self.replay[i] = self:GetChild("btnReview"..i)
        self.replay[i].visible = false
    end

    self.ceCtrl= self:GetController("c1")
    self.titleTip = self:GetChild("tips2")
    self:ClsoeRank()
    self.guesseffect.visible = false
    self.myPower.text = getNumString(logicMgr.HeroManager.GetHerosSumPower())
    self.roleName.text = G.serverData["roleInfo"].show.name
    self.place.text = logicMgr.LBSManager.GetLocationName()
    groupId = logicMgr.FriendBattleManager.GetGroup()
    self.ceCtrl.selectedIndex = 1
    --ceDay,ceTime = logicMgr.FriendBattleManager.FirstOpenTime()
    --self.dayTxt.text = ceDay
    self.nums = decodeJsonStrToTabel(G.dataTable["t_global"]["159"]["f_string_value"])
    self.placeTip.text = getLanguage("Place_0"..groupId)
    self.roadTxt.text = getLanguage("Place_0"..groupId)
    guessTimes = G.dataTable["t_gLobal"]["151"]["f_int_value"]
    battleTime = G.dataTable["t_gLobal"]["152"]["f_int_value"]
    battleTimes = G.dataTable["t_global"]["154"].f_int_value
    roleId = G.serverData["roleInfo"].show.playerId
    self.herotip.text = "助战英雄"
    local defendBtn = self:GetChild("defend");
    UIRedDots.addViewListen(PanelNames.UIFriendWar, "defendBtn", defendBtn, Config.RedDotsType.defFormation,EnumConst.FormationType.FriendDefend) -- 世界地图防守阵容红点

    self:UpdateHead()
    self:RegisterOnClick(self:GetChild("groadinfo"),self.ShowTips)
    self:RegisterOnClick(self:GetChild("defend"),self.DefenSeaTeam)
    self:RegisterOnClick(self:GetChild("BackBtn"),self.ClickBack)
    self:RegisterOnClick(self:GetChild("RankBtn"),self.ClickRank)
    self:RegisterOnClick(self:GetChild("rankback"),self.ClsoeRank)
    self:RegisterOnClick(self:GetChild("RecordBtn"),self.BattleReCord)
    self:RegisterOnClick(self:GetChild("RecordBtn2"),self.ChangeEnemy)
    self:RegisterOnClick(self.honourRoad,self.HonourRoad)
    self:RegisterOnClick(self:GetChild("rankbtn3"),self.Final)
    self:RegisterOnClick(self:GetChild("rankbtn2"),self.NextGame)
    self:RegisterOnClick(self:GetChild("rankbtn1"),self.SeaChoose)
    self:RegisterOnClick(self:GetChild("borrow1"),self.ClickBorrow)
    self:RegisterOnClick(self:GetChild("borrow2"),self.ClickBorrow)
    self:RegisterOnClick(self:GetChild("guessbtn"),self.GuessPanel)
    self:RegisterOnClick(self:GetChild("wards"),function()
        if maxindex then
            showUI(PanelNames.UIFriendWarReward,maxindex)
        else
            UISysTips.AddMsg("赛季未开始")
            return
        end
    end)
    self:RegisterOnClick(self.btn_season ,function ()
        self:DefenTeam()
    end)
    self:RegisterOnClick(self.replay[8],function ()
        if guessplayer[3] and table.getn(guessplayer[3]) > 0 then
            self:PlayRecord(1)
        end
    end)
    self:RegisterOnClick(self.replay[9],function ()
        if guessplayer[6] and table.getn(guessplayer[6]) > 0 then
            self:PlayRecord(2)
        end
    end)
    for i=1,4 do
        self:RegisterOnClick(self.replay[i],function ()
            if guessplayer[3*i] and table.getn(guessplayer[3*i]) > 0 then
                self:PlayRecord(i)
            end
        end)
    end
    self:RegisterOnClick(self.replay[5],function ()
        if guessplayer[13] and table.getn(guessplayer[13]) > 0 then
            self:PlayRecord(5)
        end
    end)
    self:RegisterOnClick(self.replay[6],function ()
        if guessplayer[14] and table.getn(guessplayer[14]) > 0 then
            self:PlayRecord(6)
        end
    end)
    self:RegisterOnClick(self.replay[7],function ()
        if guessplayer[15] and table.getn(guessplayer[15]) > 0 then
            self:PlayRecord(8)
        end
    end)
    self:RegisterOnChanged(self.seasonPop,self.OnChangeSeason)
    self:RegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);
    self:RegisterEvent(Config.EventType.HistoryRankInfo,self.RefreshRoad)
    self:RegisterEvent(Config.EventType.FightResult,self.ShowFightResult)
    self:RegisterEvent(Config.EventType.EnemyInfo,self.ShowEnemy)
    self:RegisterEvent(Config.EventType.RankDataInfo,self.ShowRank)
    self:RegisterEvent(Config.EventType.MyFriendBatteInfo,self.ShowMyInfo)
    self:RegisterEvent(Config.EventType.UpdaPartner,self.ShowBorrowHero)
    self:RegisterEvent(Config.EventType.GroupInfo,self.ShowNextInfo)
    --self:RegisterEvent(Config.EventType.GroupInfo,self.ShowFinalInfo)
    self:RegisterEvent(Config.EventType.FriendBattleRecords,self.ShowRecord)
    self:RegisterEvent(Config.EventType.SeasonTime,self.CountTime)
    --[[self:RegisterEvent(Config.EventType.BuyChallengeCount,function ()
        showUI(PanelNames.UIBattleTeam,EnumConst.FightTypeConst.FriendAttack,buyPlayerId,function (formation)
            FriendBattleHandler.ReqGoToFight(buyPlayerId,formation)
        end)
        showUI(PanelNames.UITeam,EnumConst.FightTypeConst.FriendAttack)
    end)--]]
    self:RegisterEvent(Config.EventType.Fresh_Currency, self.UpdateGold)
    self:RegisterEvent(Config.EventType.Show_HeadMenu_HeadIcon, self.UpdateHead);
    self:RegisterEvent(Config.EventType.FreashRoleShowDraw, self.UpdateHead)
    self:RegisterEvent(Config.EventType.AllGroupInfo,function()
        showUI(PanelNames.UIQuizList,curRound,groupNum)
    end)
    FriendBattleHandler.ReqFriendBorrowHeros(0)

    logicMgr.FuncOpenManager.refreshMainReward(self.mainReward,Config.FuncOpenType.FriendBattle)
end

function _M:UpdateHead()
    self.mainHead:ReFreshHead(logicMgr.HeroManager.GetRoleIcon(),logicMgr.HeroManager.GetRoleInfo("picFrame"),true)
end

function _M:UpdateGold()
    godNum = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.GOLD)
    self.moneyNum.text = godNum
    self.text_money.text =getNumString(logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.MONEY))
end

function _M:OnEnable()
    self:UpdateGold()
    self:UpdateHead()
    self:ClsoeRank()
    if not logicMgr.FriendBattleManager.IsJoin() then
        Util.SetPlayerPrefs(roleId .. "joinBattle", 0)
    end
    FriendBattleHandler.ReqFriendBattleInfo()
    local index,round,season = logicMgr.FriendBattleManager.StageIndex()
    if not season then
        self.honourRoad.visible = false
        return
    end
    self.tabCtrl.selectedIndex = index
    tabId = index
    maxindex = season  -1
    curIndex = season - 1
    if season <= 1 then
        self.honourRoad.visible = false
    else
        self.honourRoad.visible = true
    end
    if  index == 0 then
        if logicMgr.FriendBattleManager.IsElection() then
            --FriendBattleHandler.ReqFriendBattleInfo()
            FriendBattleHandler.ReqGoodFriendList()
        else
            --self:SetDynamicList(self.enemyList,self.ItemRender,nil,true)
            --self.enemyList.numItems = 4
            self.enemyState.selectedIndex = 1
        end
    else
        if index == 1 and round < 5 then
            self:NextGame()
        elseif index == 2 and round >= 5 then
            self:Final()
        end
    end
    self:OverTime()
end

function _M:OverTime()
    seasonTime = logicMgr.FriendBattleManager.SeasonOverTime()
    if seasonTime then
        self.dayTxt.text = timeToTimeLeft(seasonTime)
    else
        self.dayTxt.text = ""
    end
    if seasonTime then
        timerId = utimer.timeCall(function ()
            seasonTime = seasonTime - 60
            if seasonTime <= 0 then
                utimer.remove(timerId)
                timerId = nil
                self.dayTxt.visible = false
                FriendBattleHandler.ReqTimeInfo()
                FriendBattleHandler.ReqFriendBattleInfo()
                FriendBattleHandler.ReqGroupInfo(groupId)
                self.guessCtrl.selectedIndex = 2
                self.honourRoad.visible = true
            end
            self.dayTxt.text = timeToTimeLeft(seasonTime)
        end ,60000,-1)
    end
end

function _M:OnDestroy()
    self:ReleaseTimer()
    if timerId then
        utimer.remove(timerId)
        timerId = nil
    end
    self.finalInfo = nil
    self.nextInfo = nil
    self.roadObj = nil
end

function _M:Disable()
    self:ReleaseTimer()
    if timerId then
        utimer.remove(timerId)
        timerId = nil
    end
    self.finalInfo = nil
    self.nextInfo = nil
    self.roadObj = nil
    UIRedDots.removeViewEvent(PanelNames.UIFriendWar)
end

function _M:ReleaseTimer()
    if teamTime then
        utimer.remove(teamTime)
        teamTime = nil
    end
    if guessTime then
        utimer.remove(guessTime)
        guessTime = nil
    end
    if roundTime then
        utimer.remove(roundTime)
        roundTime = nil
    end
end
function _M:SetRound()
    outInfo = logicMgr.FriendBattleManager.GetSeasonTimeInfo()
    if not outInfo then
        return
    end
    roundStr = 2^(7-outInfo.weedOutRound)
    roundNum = roundStr
    if roundNum == 128 then
        if self.tabCtrl.selectedIndex == 1 then
            roundStr = 64
        elseif self.tabCtrl.selectedIndex == 2 then
            roundStr = 8
        end
    end
    if roundNum == 2 then
        roundStr = "决赛"
    else
        if roundStr then
            local str = roundStr.."强"
            roundStr = str
        end
    end
    curRound = outInfo.weedOutRound
    groupNum = outInfo.weedOutGroup
    self.roundTxt.text = roundStr
    self.groupTxt.text = groupNum
    self.strongTxt.text = roundStr
end
function _M:CountTime()
    outInfo = logicMgr.FriendBattleManager.GetSeasonTimeInfo()
    if not outInfo then
        return
    end
    self:SetRound()
    local rouTime,lockTime = logicMgr.FriendBattleManager.GetRoundTime(outInfo.weedOutRound)
    --阵容锁定
    if teamTime then
        utimer.remove(teamTime)
        teamTime = nil
    end
    local teamCtrl = self:GetController("lock")
    local weedOutStatus = outInfo.weedOutStatus
    if weedOutStatus == 1 then
        local countlock = tonumber(tostring(outInfo.weedOutStatusStartTime)) + lockTime - HelperFunc.GetServerTime()
        if countlock > 0 then
            self.guessCtrl.selectedIndex = 2
            teamCtrl.selectedIndex = 0
            self.betTip.visible = false
            self.guessTxt.visible = false
            self.teamTime.text = timeToTimeFormat(countlock)
            teamTime = utimer.timeCall(function ()
                countlock = countlock -1
                if countlock <= 0 then
                    teamCtrl.selectedIndex = 1
                    utimer.remove(teamTime)
                    teamTime = nil
                    FriendBattleHandler.ReqTimeInfo()
                    FriendBattleHandler.ReqFriendBattleInfo()
                    FriendBattleHandler.ReqGroupInfo(groupId)
                    --self:BattleStage(tonumber(tostring(outInfo.weedOutStatusStartTime)))
                end
                self.teamTime.text = timeToTimeFormat(countlock)
            end,1000,-1)
        else
            teamCtrl.selectedIndex = 1
        end
    else
        if weedOutStatus == 2 then
            fightState = outInfo.weedOutFightStatus
            teamCtrl.selectedIndex = 1
            self:BattleStage(tonumber(tostring(outInfo.weedOutFightStatusStartTime)))
        else
            self.guessCtrl.selectedIndex = 2
            teamCtrl.selectedIndex = 1
        end
    end
    --淘汰
    if roundTime then
        utimer.remove(roundTime)
        roundTime = nil
    end
    local outTime = 0
    if weedOutStatus ~= 1 then
        outTime =int64ToNumber(outInfo.weedOutStatusStartTime) + rouTime - HelperFunc.GetServerTime()-lockTime
    else
        outTime =int64ToNumber(outInfo.weedOutStatusStartTime) + rouTime - HelperFunc.GetServerTime()
    end
    if outTime > 0 then
        self.strongTime.text = timeToTimeFormat(outTime)
        roundTime = utimer.timeCall(function ()
            outTime = outTime - 1
            if outTime <= 0 then
                FriendBattleHandler.ReqTimeInfo()
                FriendBattleHandler.ReqGroupInfo(groupId)
                utimer.remove(roundTime)
                roundTime = nil
            end
            self.strongTime.text = timeToTimeFormat(outTime)
        end,1000,-1)
    else
        self.strongTime.text = "00:00:00"
    end
end

function _M:BattleStage(tempTime)
    self.betTip.visible = true
    self.guessTxt.visible = true
    if guessTime then
        utimer.remove(guessTime)
        guessTime = nil
    end
    local time = (HelperFunc.GetServerTime() - tempTime) % (guessTimes+battleTime)
    if not (fightState and fightState ==2) and (not frameIds[15]) then
        if time < guessTimes then
            self.guessCtrl.selectedIndex = 0
            nextTime = guessTimes - time
            self.guessTxt.text = ""
        elseif time < guessTimes + battleTime then
            self.guessCtrl.selectedIndex = 1
        end
        guessTime = utimer.timeCall(function ()
            time = time +1
            if time < guessTimes then
                self.guessCtrl.selectedIndex = 0
                nextTime = guessTimes - time
                self.guessTxt.text = ""
            else
                FriendBattleHandler.ReqTimeInfo()
                FriendBattleHandler.ReqGroupInfo(groupId)
                time = 0
            end
        end,1000,-1)
    else
        self.guessCtrl.selectedIndex = 1
        guessTime = utimer.timeCall(function ()
            time = time +1
            if time > battleTime then
                --FriendBattleHandler.ReqFriendBattleInfo()
                FriendBattleHandler.ReqTimeInfo()
                FriendBattleHandler.ReqGroupInfo(groupId)
                utimer.remove(guessTime)
                guessTime = nil
            end
        end,1000,-1)
    end
end

function _M:ShowBorrowHero()
    UISysTips.AddMsg("设置出借英雄成功")
    local borrows1,borrows2 =  logicMgr.FriendBattleManager.GetBorrowHero()
    if borrows1 then
        self.hero_L.url = EnumConst.AtlasPath.ICONAtlas..logicMgr.HeroManager.getHeroDatabyPartnerId(borrows1,true)["icon"]
    else
        self.hero_L.url = ""
    end
    if borrows2 then
        self.hero_R.url =EnumConst.AtlasPath.ICONAtlas..logicMgr.HeroManager.getHeroDatabyPartnerId(borrows2,true)["icon"]
    else
        self.hero_R.url = ""
    end
end

function _M:ShowMyInfo()
    self.defendPower.text = getNumString(logicMgr.FriendBattleManager.GetDefPower())
    local myInfo = logicMgr.FriendBattleManager.GetFriendChallengeList()
    if logicMgr.FriendBattleManager.IsJoin() then
        self.hero_L_Ctrl.selectedIndex = 0
        self.hero_R_Ctrl.selectedIndex = 0
     else
        self.hero_L_Ctrl.selectedIndex = 2
        self.hero_R_Ctrl.selectedIndex = 2
    end
    if myInfo then
        battleTimes = G.dataTable["t_global"]["154"].f_int_value
        battleTimes = battleTimes - myInfo.battleTimes
        buyTimes = myInfo.buyTimes
        if battleTimes <= 0 then
            self.challengeNum.text = #self.nums - buyTimes
            self.changeTitle.text = "海选赛红魔石挑战剩余次数"
        else
            self.challengeNum.text = battleTimes
            self.changeTitle.text = "海选赛免费挑战剩余次数"
        end
        self.rankNum.text = tostring(myInfo.rank)
        rankIndex = tonumber(tostring(myInfo.rank))
        self.rankScore.text = tostring(myInfo.score)
        myScore = tostring(myInfo.score)
        if myInfo.partner[1] then
            self.hero_L.url = EnumConst.AtlasPath.ICONAtlas..logicMgr.HeroManager.getHeroDatabyPartnerId(myInfo.partner[1],true)["icon"]
        end
        if myInfo.partner[2] then
            self.hero_R.url =EnumConst.AtlasPath.ICONAtlas..logicMgr.HeroManager.getHeroDatabyPartnerId(myInfo.partner[2],true)["icon"]
        end
    end
end

function _M:ShowEnemy()
    self.enemyInfo = logicMgr.FriendBattleManager.GetChallengeFriendList()
    if self.enemyInfo and #self.enemyInfo> 0 then
        self:SetDynamicList(self.enemyList,self.ItemRender,nil,true)
        self.enemyList.numItems = #self.enemyInfo
        self.enemyState.selectedIndex = 0
    else
        --self:SetDynamicList(self.enemyList,self.ItemRender)
        --self.enemyList.numItems = 4
        if tabId >= 1 then
            self.titleTip.text = "海选赛已结束..."
        else
            self.titleTip.text = "正在布阵..."
        end
        self.enemyState.selectedIndex = 1
    end
end

function _M:ItemRender(index,obj)
    local state = obj:GetController("c1")
    if not logicMgr.FriendBattleManager.IsJoin() then
        local config = self.enemyInfo[index]
        if config then
            state.selectedIndex = 1
            local name = obj:GetChild("PlayName")
            local power = obj:GetChild("PlayNametext")
            local icon = obj:GetChild("PlayHead")
            local score = obj:GetChild("goal")
            local btngo = obj:GetChild("btngo")
            local freeCtrl = obj:GetController("freeCtrl")
            local model = obj:GetChild("model")
            local posCtrl = obj:GetController("position")
            if index == 3 then
                posCtrl.selectedIndex = 2
            elseif index == 1 or index == 5 then
                posCtrl.selectedIndex = 0
            else
                posCtrl.selectedIndex = 1
            end
            score.text = tostring(config.score)
            name.text = config.name
            power.text = getNumString(config.defTotalPower)
            icon.url = "ui://HalfBodyAtlas/hero_halfbody_"..config.pic
            local config2 = G.dataTable["t_skin"][config.pic]
            local path = logicMgr.SkinManager.GetHeroModelPathBySkinId(config.pic)
            resMgr:LoadModelPrefab(path, function(go)
                wraps[index] = nil
                wraps[index] = GoWrapper.New()
                if mobj[index] then
                    destroy(mobj[index])
                end
                mobj[index] = newObject(go);
                mobj[index]:SetActive(true);
                mobj[index]:AddComponent(TCanvas);
                mobj[index]:AddComponent(TChangeSortingOrder)
                local aniObj = mobj[index].transform:Find("Animation").gameObject;
                logicMgr.EquipManager.setWeaponSkin(aniObj, config2.f_HeroID, true);
                --[[local skinName = (G.dataTable["t_roleconfig"][config.f_HeroID] or {}).skin;
                if skinName ~= nil and skinName ~= '' then
                    Util.SetSkin(aniObj, skinName);
                end--]]
                wraps[index]:SetWrapTarget(mobj[index], false)
                model:SetNativeObject(wraps[index])
                Util.Lua_SetLocalPositionXYZ(mobj[index].transform, 0,-50, 0)
                Util.Lua_SetLocalScaleXYZ(mobj[index].transform, 100, 100, 100)
                Util.Lua_SetLocalRotationXYZ(aniObj.transform, 0, 0, 0)
            end)
            local tip = obj:GetChild("tip")
            if logicMgr.FriendBattleManager.IsElection() then
                if battleTimes <= 0 then
                    if buyTimes < #self.nums then
                        freeCtrl.selectedIndex = 1
                        local costTxt = obj:GetChild("title")
                        costNum = self.nums[buyTimes+1]
                        costTxt.text = costNum
                        isFree = false
                    else
                        freeCtrl.selectedIndex = 0
                        tip.text = getLanguage("FriendWarTimes_2")
                    end
                else
                    isFree = true
                    tip.text = getLanguage("FriendWarTimes_1")
                    freeCtrl.selectedIndex = 0
                end
                btngo.onClick:Clear()
                self:RegisterOnClick(btngo,function ()
                    if not isFree then
                        if buyTimes < #self.nums then
                            if godNum >= self.nums[buyTimes+1] then
                                local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop)
                                pop.Init("23", function(b)
                                    if b then
                                        buyPlayerId = config.playerId
                                        logicMgr.FriendBattleManager.SetCurrentFightPlayer(config.playerId)
                                        showUI(PanelNames.UIBattleTeam,EnumConst.FightTypeConst.FriendAttack,buyPlayerId,function (formation)
                                            FriendBattleHandler.ReqBuyChallengeCount(1)
                                            FriendBattleHandler.ReqGoToFight(buyPlayerId,formation)
                                        end)
                                        showUI(PanelNames.UITeam,EnumConst.FightTypeConst.FriendAttack)

                                        self:UploadFightRecord(config.playerId)
                                    end
                                end,costNum);
                            else
                                UISysTips.AddMsg(getLanguage("Character_Tips7"))
                                return
                            end
                        else
                            UISysTips.AddMsg("挑战次数已用尽")
                            return
                        end
                    else
                        logicMgr.FriendBattleManager.SetCurrentFightPlayer(config.playerId)
                        showUI(PanelNames.UIBattleTeam,EnumConst.FightTypeConst.FriendAttack,config.playerId,function (formation)
                            FriendBattleHandler.ReqGoToFight(config.playerId,formation)
                        end)
                        showUI(PanelNames.UITeam,EnumConst.FightTypeConst.FriendAttack)
                        self:UploadFightRecord(config.playerId)
                    end
                end)
            else
                local rank = obj:GetChild("title1")
                rank.text = tostring(config.rank)
                state.selectedIndex = 2
            end
        else
            state.selectedIndex = 0
        end
    else
        state.selectedIndex = 0
    end
end

function _M:ShowRank()
    self.rankInfo =  logicMgr.FriendBattleManager.GetRankData()
    self.rankCtrl.selectedIndex = 1
    self:SetDynamicList(self.rankList,self.RankData)
    self.rankList.numItems = #self.rankInfo
    local myName = self:GetChild("myhead/name")
    local myLevel = self:GetChild("myhead/level")
    local myRankNum = self:GetChild("myhead/title")
    local myPoint = self:GetChild("myhead/point")
    local myCtrl = self:GetController("rank",self:GetChild("myhead"))
    myName.text = G.serverData["roleInfo"].show.name
    myLevel.text = G.serverData["roleInfo"].show.level
    myPoint.text = myScore
    if rankIndex > 4 then
        myCtrl.selectedIndex = 4
        myRankNum.text = rankIndex
    else
        myCtrl.selectedIndex = rankIndex - 1
    end
    local myHead = self:CreateComponent(PlayerHeadFrame,self:GetChild("myhead/head"))
    myHead:ReFreshHead( logicMgr.HeroManager.GetRoleInfo("pic"), logicMgr.HeroManager.GetRoleInfo("picFrame"))
end

function _M:RankData(index,obj)
    local config = self.rankInfo[index]
    local name = obj:GetChild("name")
    local level = obj:GetChild("level")
    local score = obj:GetChild("point")
    local rankNum = obj:GetChild("title")
    local rankCtrl = obj:GetController("rank")
    local type = obj:GetController("type")
    local head = self:CreateComponent(PlayerHeadFrame,obj:GetChild("head"))
    head:ReFreshHead(config.pic,config.picFrame)
    obj:GetChild("head").onClick:Clear()
    obj:GetChild("head").onClick:Add(function ()
        if tostring(config.playerId) == HeroManager.GetRoleId() then
            return ;
        end
        roleInfoHandler.sendreqOtherRoleInfo(config.playerId);
    end);
    name.text = config.name
    level.text = config.lv
    score.text = tostring(config.score)
    if config.rank > 4 then
        rankCtrl.selectedIndex = 4
        rankNum.text = config.rank
    else
        rankCtrl.selectedIndex = config.rank - 1
    end
end

function _M:GotOtherShowInfo(info)
    showUI(PanelNames.UIPlayerShow, info,nil,false,function ()
        UIBottom.enterAdventure()
        --showUI(PanelNames.UIArenaMainEnter);
        showUI(PanelNames.UIFriendWar)
    end)
end

function _M:ShowRecord()
    recordInfo = logicMgr.FriendBattleManager.GetBattleRecord()
    table.sort(recordInfo,function(a, b)
        if tonumber(tostring(a.createTime)) >= tonumber(tostring(b.createTime)) then
            return true
        end
    end)
    self:SetDynamicList(self.reviewList,self.RecordData)
    if #recordInfo==0 then
        self.battleCordCtrl.selectedIndex = 1
    else
        self.reviewList.numItems = #recordInfo
    end
end

function _M:RecordData(index,obj)
    local config = recordInfo[index]
    local btnReview = obj:GetChild("btnReview")
    local resultCtrl = obj:GetController("winOrLose")
    if tostring(config.attacker.playerId) == logicMgr.HeroManager.GetRoleId() then
        resultCtrl.selectedIndex = 1 - config.win
    else
        resultCtrl.selectedIndex = config.win
    end
    local head_L = self:CreateComponent(PlayerHeadFrame,obj:GetChild("headLeft"))
    head_L:ReFreshHead(config.attacker.pic,config.attacker.picFrame)
    local name_L = obj:GetChild("playerNameLeft")
    name_L.text = config.attacker.playerName
    local rankNum_L = obj:GetChild("playRankLeft")
    rankNum_L.text = tostring(config.attacker.rank)
    local scoreNum_L = obj:GetChild("playPointLeft")
    scoreNum_L.text = tostring(config.attacker.score)
    local head_R= self:CreateComponent(PlayerHeadFrame,obj:GetChild("headRight"))
    head_R:ReFreshHead(config.defender.pic,config.defender.picFrame)
    local name_R = obj:GetChild("playerNameRight")
    name_R.text = config.defender.playerName
    local rankNum_R = obj:GetChild("playRankRight")
    rankNum_R.text = tostring(config.defender.rank)
    local scoreNum_R = obj:GetChild("playPointRight")
    scoreNum_R.text = tostring(config.defender.score)
    btnReview.onClick:Clear()
    btnReview.onClick:Add(function ()
        BattlePlaybackManager.setBattleRecord(config)
        BattlePlaybackManager.setBattleResult(config.win)
        BattlePlaybackManager.requestFightCmdThenPlay(config, EnumConst.FightTypeConst.FriendAttack,function () 
            FightManager.SetRePlay(true)
        end);
    end)
end
----------------------------onClick-----------------------------------
function _M:ClickRank()
    --[[if ceTime > 0 then
        logicMgr.FriendBattleManager.OpenTip()
        return
    end--]]
    FriendBattleHandler.ReqRankData(0,groupId)
end

function _M:ClickBorrow()
    if logicMgr.FriendBattleManager.IsJoin() then
        showUI(PanelNames.UILend)
    else
        UISysTips.AddMsg("报名期才能设置助战英雄")
    end
end

function _M:ShowFinalInfo()
    if self.tabCtrl.selectedIndex == 2 then
        for i=1,17 do
            self.finalInfo[i]["obj"].onClick:Clear()
        end
        frameIds = {}
        local groupInfo,info_3rd = logicMgr.FriendBattleManager.SetGroupInfo()
        for i = 1,15 do
            self.finalInfo[i]["line"].visible = false
            self.finalInfo[i]["obj"].grayed = false
        end
        if groupInfo and (curRound > 4) then
            guessplayer = groupInfo
            if info_3rd then
                inford  = info_3rd
                local ctrl = self:GetController("fight")
                if groupNum == 3 then
                    ctrl.selectedIndex = 1
                    self:SetPlayerInfo(self.finalInfo[16],info_3rd.p1)
                    self:SetPlayerInfo(self.finalInfo[17],info_3rd.p2)
                    isThrd = true
                    guessplayer_1 = 12
                    guessplayer_2 = 12
                else
                    isThrd = false
                    ctrl.selectedIndex = 0
                    local winnerId = tostring(info_3rd.winner)
                    local fourId
                    if info_3rd.p1 then
                        fourId = tostring(info_3rd.p1.playerId)
                    end
                    if fourId then
                        if winnerId == fourId then
                            fourId = tostring(info_3rd.p2.playerId)
                        end
                    end
                    if groupNum >= 4 then
                        for i=3,12,3 do
                            if tostring(groupInfo[i].playerId) == winnerId then
                                local objIndex = self:GetChild("num"..i.."-".."3")
                                objIndex.visible = true
                                local frameId = logicMgr.FriendBattleManager.GetSeasonFrameId(curIndex+1,3)
                                frameIds[i] = frameId
                            end
                            if tostring(groupInfo[i].playerId) == fourId  then
                                local objIndex = self:GetChild("num"..i.."-".."4")
                                objIndex.visible = true
                                local frameId = logicMgr.FriendBattleManager.GetSeasonFrameId(curIndex+1,4)
                                frameIds[i] = frameId
                            end
                        end
                    end
                end
            end
            if not teamTime then
                local indexs = logicMgr.FriendBattleManager.GetLineIndex(groupInfo)
                if not indexs then
                    indexs = 0
                end
                if indexs < 12 then
                    if self.finalInfo[indexs+2]["line"] then
                        self.finalInfo[indexs+1]["line"].visible = true
                        self.finalInfo[indexs+2]["line"].visible = true
                    end
                    guessplayer_1 = indexs + 1
                    guessplayer_2 = indexs + 2
                else
                    if indexs == 12 then
                        guessplayer_1 = 10
                        guessplayer_2 = 11
                    end
                    if indexs == 13 then
                        self.finalInfo[3]["line"].visible = true
                        self.finalInfo[12]["line"].visible = true
                        guessplayer_1 = 3
                        guessplayer_2 = 12
                    end
                    if indexs == 14 then
                        self.finalInfo[6]["line"].visible = true
                        self.finalInfo[9]["line"].visible = true
                        guessplayer_1 = 6
                        guessplayer_2 = 9
                    end
                    if indexs == 15 and groupNum ~= 3   then
                        self.finalInfo[13]["line"].visible = true
                        self.finalInfo[14]["line"].visible = true
                        guessplayer_1 = 13
                        guessplayer_2 = 14
                    end
                end
            end
            if groupInfo[15] and groupInfo[15].playerId and tonumber(tostring(groupInfo[15].playerId)) ~= 0 and self.guessCtrl.selectedIndex ~= 1 then
                local objNum1 = self:GetChild("num15-1")
                local id = logicMgr.FriendBattleManager.GetSeasonFrameId(curIndex+1,1)
                frameIds[15] = id
                local objNum2 = nil;
                objNum1.visible = true
                if tostring(groupInfo[15].playerId) == tostring(groupInfo[14].playerId) then
                    objNum2 = self:GetChild("num13-2")
                    local frameId = logicMgr.FriendBattleManager.GetSeasonFrameId(curIndex+1,2)
                    frameIds[13] = frameId
                else
                    objNum2 = self:GetChild("num14-2")
                    local frameId = logicMgr.FriendBattleManager.GetSeasonFrameId(curIndex+1,2)
                    frameIds[14] = frameId
                end
                objNum2.visible = true
            end
            for i=1,15 do
                local index = 0
                local grayIndex = nil
                if index % 3 == 0 or (i > 12 and i< 15) then
                    if i == 13 then
                        index = logicMgr.FriendBattleManager.IsBattleWinner(groupInfo[13],groupInfo[3],groupInfo[12],i)
                        if index then
                            grayIndex = i-(11-index)
                        end
                    elseif i == 14 then
                        index = logicMgr.FriendBattleManager.IsBattleWinner(groupInfo[14],groupInfo[6],groupInfo[9],i)
                        if index then
                            grayIndex = i-(13-index)
                        end
                    else
                        index = logicMgr.FriendBattleManager.IsBattleWinner(groupInfo[i],groupInfo[i-1],groupInfo[i-2])
                        if index then
                            grayIndex = i-(3-index)
                        end
                    end
                    if index then
                        self.finalInfo[i-index]["line"].visible = true
                    end
                    if grayIndex then
                        self.finalInfo[grayIndex]["obj"].grayed = true
                    end
                end
                self:SetPlayerInfo(self.finalInfo[i],groupInfo[i],i)
                if frameIds[15] then
                    self.guessCtrl.selectedIndex = 2
                    self.btn_season.visible = false
                end
                self.finalInfo[i]["obj"].onClick:Clear()
                self:RegisterOnClick(self.finalInfo[i]["obj"],function()
                    if groupInfo[i] and table.getn(groupInfo[i]) > 0 then
                        showUI(PanelNames.UIGuessInformation,groupInfo[i])
                    else
                        UISysTips.AddMsg(getLanguage("FriendwarTips7"))
                    end
                end)
            end
            for i=3,12,3 do
                self:ShowBtnPlay(groupInfo[i],groupInfo[i-2],groupInfo[i-1],i/3)
            end
            self:ShowBtnPlay(groupInfo[13],groupInfo[12],groupInfo[3],5)
            self:ShowBtnPlay(groupInfo[14],groupInfo[9],groupInfo[6],6)
            self:ShowBtnPlay(groupInfo[15],groupInfo[14],groupInfo[13],7)
        end
        if curRound >= 5 then
            if (not teamTime) and (not frameIds[15]) then
                self.guesseffect.visible = true
            else
                self.guesseffect.visible = false
            end
        else
            self.guesseffect.visible = false
        end
    end
end

function _M:Final()
    if not self.finalInfo then
        self.finalInfo = {}
        for i=1,17 do
            self.finalInfo[i] = {}
            local obj =  self:GetChild("playheadF"..i)
            self.finalInfo[i]["obj"] = obj
            self.finalInfo[i]["name"] = obj:GetChild("title3")
            self.finalInfo[i]["level"] = obj:GetChild("title")
            self.finalInfo[i]["head"] = self:CreateComponent(PlayerHeadFrame,obj:GetChild("head"))
            self.finalInfo[i]["title"] = obj:GetChild("title1")
            self.finalInfo[i]["titleCtrl"] = obj:GetController("c1")
            if i > 14 then
                self.finalInfo[i]["line"] = self:GetChild("lineF14")
            else
                self.finalInfo[i]["line"] = self:GetChild("lineF"..i)
            end
            self.finalInfo[i]["line"].visible = false
            self.finalInfo[i]["obj"].grayed = false
        end
    end
    self.betTip.visible = true
    self.guessTxt.visible = true
    --self:ReleaseTimer()
    self.tabCtrl.selectedIndex = 2
    self:SetRound()
    self:FinalTeamSet()
    if curRound <= 4 then
        self.roundTxt.text = "4强"
        self.groupTxt.text = 0
        self.strongTxt.text = "4强"
        self.betTip.visible = false
        self.guessTxt.visible = false
    end
    seasonTime = logicMgr.FriendBattleManager.SeasonOverTime()
    if logicMgr.FriendBattleManager.IsNext() and curRound > 4 then
        FriendBattleHandler.ReqTimeInfo()
        FriendBattleHandler.ReqGroupInfo(groupId)
    else
        UISysTips.AddMsg("非决赛时间")
        return
    end
end

function _M:FinalTeamSet()
    for i=1,15 do
        if i< 13 then
            if i % 3 == 0 then
                self.finalInfo[i]["title"].text = "4强"
            else
                self.finalInfo[i]["title"].text = "8强"
            end
        else
            self.finalInfo[13]["title"].text = "亚军"
            self.finalInfo[14]["title"].text = "亚军"
            self.finalInfo[15]["title"].text = "冠军"
        end
    end
end

function _M:HonourRoad()
    seasonIndex = 0
    FriendBattleHandler.ReqHistoryRank(tonumber(maxindex),tonumber(groupId))
    self.tabCtrl.selectedIndex = 3
    --maxindex = seasonIndex
    self.popSeason = self:GetChild("seasonPop")
    self.popSeason.itemList:Clear()
    for i=maxindex,1,-1 do
        self.popSeason.itemList:Add("第"..i.."赛季")
    end
    self.seasonPop.selectedIndex = 0
end

function _M:OnChangeSeason()
    local index = self.seasonPop.selectedIndex
    seasonIndex = index
    FriendBattleHandler.ReqHistoryRank(tonumber(maxindex-index),tonumber(groupId))
end

function _M:RefreshRoad()
    local fourInfo,_ = logicMgr.FriendBattleManager.GetHistoryRank()
    if not self.roadObj then
        self.roadObj = {}
        for i = 1,4 do
            self.roadObj[i] = {}
            self.roadObj[i]["obj"] = self:GetChild("play"..i)
            self.roadObj[i]["head"] = self:CreateComponent(PlayerHeadFrame,self:GetChild("play"..i))
            self.roadObj[i]["head"]:ReFreshHead(nil,nil)
            self.roadObj[i]["level"] = self:GetChild("PlayerLv"..i)
            self.roadObj[i]["level"].text = ""
            self.roadObj[i]["name"]= self:GetChild("Playername"..i)
            self.roadObj[i]["name"].text = ""
        end
    end
    if #fourInfo > 0 then
        for i = 1,4 do
            local config = fourInfo[i]
            if config then
                self.roadObj[i]["level"].text = config.lv
                self.roadObj[i]["name"].text = config.name
                local frameId = logicMgr.FriendBattleManager.GetSeasonFrameId(maxindex - seasonIndex,i)
                self.roadObj[i]["head"]:ReFreshHead(config.pic,frameId)
                self.roadObj[i]["obj"].onClick:Clear();
                self.roadObj[i]["obj"].onClick:Add(function()
                    if tostring(config.playerId) ~= logicMgr.HeroManager.GetRoleId() then
                        roleInfoHandler.sendreqOtherRoleInfo(config.playerId);
                    end
                end)
            end
        end
    end
    self:RegisterOnClick(self:GetChild("btnSure"),function ()
        showUI(PanelNames.UIRankList)
    end)
end

function _M:SeaChoose()
    local info = logicMgr.FriendBattleManager.GetSeasonTimeInfo()
    if logicMgr.FriendBattleManager.IsNext() then
        if info.apply == 0 then
            UISysTips.AddMsg(getLanguage("FriendwarTips9"))
            return
        end
    end
    self.tabCtrl.selectedIndex = 0
    self.rankCtrl.selectedIndex = 0
    --self:ReleaseTimer()
    seasonTime = logicMgr.FriendBattleManager.SeasonOverTime()
    if not logicMgr.FriendBattleManager.IsJoin() then
        if logicMgr.FriendBattleManager.IsElection() then
            FriendBattleHandler.ReqFriendBattleInfo()
            FriendBattleHandler.ReqGoodFriendList()
        else
            FriendBattleHandler.ReqGoodFriendList()
            UISysTips.AddMsg(getLanguage("ErrorCode_3544"))
            return
        end
    end
end

function _M:NextGame()
    if not self.nextInfo then
        self.nextInfo = {}
        self.lineObj = {}
        for j=1,6 do
            self.nextInfo[j] = {}
            local obj1 =  self:GetChild("playhead"..j)
            self.nextInfo[j]["obj"] = obj1
            self.nextInfo[j]["name"] = obj1:GetChild("title3")
            self.nextInfo[j]["level"] = obj1:GetChild("title")
            self.nextInfo[j]["head"] = self:CreateComponent(PlayerHeadFrame,obj1:GetChild("head"))
            self.nextInfo[j]["title"] = obj1:GetChild("title1")
            self.nextInfo[j]["titleCtrl"] = obj1:GetController("c1")
            if j % 3 ~= 0 then
                self.nextInfo[j]["line"] = self:GetChild("line"..j)
            else
                self.nextInfo[j]["line"] = self:GetChild("line"..(j-1))
            end
            self.nextInfo[j]["line"].visible = false
            self.nextInfo[j]["obj"].grayed = false
        end
    end
    self.betTip.visible = true
    self.guessTxt.visible = true
    --self:ReleaseTimer()
    self.tabCtrl.selectedIndex = 1
    self:SetRound()
    self:NextTeamSet()
    if curRound > 4 then
        if self.tabCtrl.selectedIndex == 1 then
            self.roundTxt.text = "8强"
            self.groupTxt.text = tostring(8)
            self.strongTxt.text = "8强"
            self.betTip.visible = false
            self.guessTxt.visible = false
        end
    end
    if curRound == 0 then
        self.betTip.visible = false
        self.guessTxt.visible = false
    end
    seasonTime = logicMgr.FriendBattleManager.SeasonOverTime()
    if logicMgr.FriendBattleManager.IsNext() and curRound <= 4 then
        FriendBattleHandler.ReqTimeInfo()
        FriendBattleHandler.ReqGroupInfo(groupId)
    else
        UISysTips.AddMsg("非晋级赛时间")
        return
    end
end

function _M:NextTeamSet()
    local num = curRound
    if not num then
        num = 1
    end
    if  num > 4 then
        num = 4
    end
    local index =  2^(7-num)
    if index  > 64 then
        index = 64
    end
    for i = 1,6 do
        if i%3 == 0 then
            self.nextInfo[i]["title"].text = index.."强"
        else
            self.nextInfo[i]["title"].text = (2*index).."强"
        end
        self:SetPlayerInfo(self.nextInfo[i],nil)
    end
end

function _M:ShowNextInfo()
    --self:NextTeamSet()
    if self.tabCtrl.selectedIndex == 2 then
        self:ShowFinalInfo()
    end
    if self.tabCtrl.selectedIndex == 1 then
        for i=1,6 do
            self.nextInfo[i]["obj"].onClick:Clear()
        end
        local groupInfo = logicMgr.FriendBattleManager.SetGroupInfo()
        guessplayer = {}
        guessplayer = groupInfo
        for i = 1,6 do
            self.nextInfo[i]["obj"].grayed = false
            self.nextInfo[i]["line"].visible = false
        end
        if groupInfo and curRound <= 4 then
            local indexs = logicMgr.FriendBattleManager.GetLineIndex(groupInfo)
            if not indexs then
                indexs = 0
            end
            for i =3,6,3 do
                local num = i-2
                if logicMgr.FriendBattleManager.SetLineObj(groupInfo,i) then
                    self.nextInfo[i]["line"] = self:GetChild("line"..(i-2))
                    num = i-1
                end
                if tonumber(indexs) >= i then
                    self.nextInfo[i]["line"].visible = true
                    self.nextInfo[num]["obj"].grayed = true
                end
            end
            guessplayer_1 = indexs + 1
            guessplayer_2 = indexs + 2
            if not teamTime then
                if not indexs then
                    indexs = 0
                end
                if self.nextInfo[indexs+2] then
                    if self.nextInfo[indexs+2]["line"] then
                        self.nextInfo[indexs+1]["line"].visible = true
                        self.nextInfo[indexs+2]["line"].visible = true
                    end
                    if logicMgr.FriendBattleManager.GetLine() then
                        self.nextInfo[indexs+1]["line"].visible = false
                        self.nextInfo[indexs+2]["line"].visible = false
                    end
                end
            end
            for i=1,6 do
                self:SetPlayerInfo(self.nextInfo[i],groupInfo[i])
                self.nextInfo[i]["obj"].onClick:Clear()
                self:RegisterOnClick(self.nextInfo[i]["obj"],function()
                    if groupInfo[i] and table.getn(groupInfo[i]) > 0 then
                        showUI(PanelNames.UIGuessInformation,groupInfo[i])
                    else
                        UISysTips.AddMsg(getLanguage("FriendwarTips7"))
                    end
                end)
            end
            self:ShowBtnPlay(groupInfo[3],groupInfo[2],groupInfo[1],8)
            self:ShowBtnPlay(groupInfo[6],groupInfo[5],groupInfo[4],9)
            if frameIds[15] then
                self.guessCtrl.selectedIndex = 2
                self.btn_season.visible = false
            end
        end
        if  logicMgr.FriendBattleManager.IsNext() and curRound <= 4 then
            if not teamTime and (not frameIds[15]) then
                self.guesseffect.visible = true
            else
                self.guesseffect.visible = false
            end
        else
            self.guesseffect.visible = false
        end
    end
end

function _M:ShowBtnPlay(info_1,info_2,info_3,index)
    if info_1 and table.getn(info_1) > 0 and info_2 and table.getn(info_2) > 0 and info_3 and table.getn(info_3) > 0 then
        self.replay[index].visible = true
    else
        self.replay[index].visible = false
    end
end

function _M:PlayRecord(info)
    if (not teamTime) then
        local _,msg = logicMgr.FriendBattleManager.GetGroupUid(nil,info)
        if msg and msg.record then
            if msg.record.version ==  GetFightVersion() then
                BattlePlaybackManager.setBattleRecord(msg.record)
                BattlePlaybackManager.setBattleResult(msg.record.win)
                BattlePlaybackManager.requestFightCmdThenPlay(msg.record, EnumConst.FightTypeConst.FriendAttack,function () FightManager.SetRePlay(true) end)
            else
                UISysTips.AddMsg(getLanguage("ErrorCode_220005"))
            end
        end
    end
end

function _M:SetPlayerInfo(obj,config,index)
    if config and table.getn(config) > 0 then
        obj.name.text = config.name
        obj.level.text = config.lv
        obj.titleCtrl.selectedIndex = 0
        local frameId = config.picFrame
        if index and frameIds[index] then
            frameId = frameIds[index]
        end
        obj.head:ReFreshHead(config.pic,frameId)
    else
        obj.name.text = ""
        obj.level.text = ""
        obj.titleCtrl.selectedIndex = 1
        obj.head:ReFreshHead(nil,nil)
    end
end

function _M:BattleReCord()
    --[[if ceTime > 0 then
        logicMgr.FriendBattleManager.OpenTip()
        return
    end--]]
    self.fightRecord.selectedIndex = 1
    FriendBattleHandler.ReqBattleRecord()
end

function _M:ChangeEnemy()
    --[[if ceTime > 0 then
        logicMgr.FriendBattleManager.OpenTip()
        return
    end--]]
    if refreshTimeNum <= 0 then
        FriendBattleHandler.ReqGoodFriendList()
        refreshTimeNum = tonumber(G.dataTable["t_global"]["71"]["f_int_value"]) / 1000;
        if timeRefresh == nil then
            timeRefresh = Timer.New(function()
                refreshTimeNum = 0;
            end, refreshTimeNum, 1);
        else
            timeRefresh:Reset(function()
                refreshTimeNum = 0;
            end, refreshTimeNum, 1);
        end
        timeRefresh:Start();
    else
        UISysTips.AddMsg(string.format(getLanguage("Character_Tips11")));
    end
end

function _M:ClsoeRank()
    self.rankCtrl.selectedIndex = 0
end

function _M:GuessPanel()
    if tabId >=1 and (not teamTime) then
        FriendBattleHandler.ReqGroupInfo(groupId,curRound)
        logicMgr.FriendBattleManager.SetEventType(true)
    else
        UISysTips.AddMsg(getLanguage("FriendwarTips5"))
        return
    end
end

function _M:ShowTips()
    local txts = {}
    txts["title"] = "FriendWarTitle"
    txts["des"] = "FriendWarText"
    showUI(PanelNames.CommonTips, txts)
end

function _M:DefenTeam()
    --[[if teamTime then

    else
        UISysTips.AddMsg("准备期才能设置防守阵容")
    end--]]
    --[[if ceTime > 0 then
        logicMgr.FriendBattleManager.OpenTip()
        return
    end--]]
    if logicMgr.FriendBattleManager.IsGoTeam() then
        showUI(PanelNames.UIBattleTeam,EnumConst.FightTypeConst.FriendDefend,nil,function (formation)
            FriendBattleHandler.ReqDefFormation(formation)
        end);
        showUI(PanelNames.UITeam,EnumConst.FightTypeConst.FriendDefend)
    else
        UISysTips.AddMsg("好友战未报名")
        return
    end
end

function _M:DefenSeaTeam()
    --[[if teamTime then

    else
        UISysTips.AddMsg("准备期才能设置防守阵容")
    end--]]
    --[[if ceTime > 0 then
        logicMgr.FriendBattleManager.OpenTip()
        return
    end--]]
    if logicMgr.FriendBattleManager.IsGoTeam() then
        if tabId >= 1 then
            UISysTips.AddMsg(getLanguage("FriendwarTips3"))
            return
        end
        showUI(PanelNames.UIBattleTeam,EnumConst.FightTypeConst.FriendDefend,nil,function (formation)
            FriendBattleHandler.ReqDefFormation(formation)
        end);
        showUI(PanelNames.UITeam,EnumConst.FightTypeConst.FriendDefend)
    else
        UISysTips.AddMsg("好友战未报名")
        return
    end
end

--好友挑战埋点
function _M:UploadFightRecord(rid)
    TAManager.TrackByDictionary("click_friend_battle", {role_id = tostring(rid)})
end

function _M:ClickBack()
    closeUI(PanelNames.UIFriendWar)
    --closeUI(PanelNames.UIHeadMenu)
    logicMgr.FriendBattleManager.ClearRank()
end
----------------------------onClick-----------------------------------
return _M

